#!/bin/bash

DIRECTORY="$(dirname "$(dirname "$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )")")"

function error {
  echo -e "\\e[91m$1\\e[39m"
  exit 1
}

wget -O ~/Cura.AppImage https://github.com/smartavionics/Cura/releases/download/20210908/Cura-mb-master-armhf-20210908.AppImage || error "Failed to download!"

chmod +x ~/Cura.AppImage

#get stl-thumb package
rm -f ~/stl-thumb_0.4.0_armhf.deb
wget https://github.com/unlimitedbacon/stl-thumb/releases/download/v0.4.0/stl-thumb_0.4.0_armhf.deb
"${DIRECTORY}/pkg-install" "$HOME/stl-thumb_0.4.0_armhf.deb" "$(dirname "$0")" || exit 1
rm -f ~/stl-thumb_0.4.0_armhf.deb

echo "[Desktop Entry]
Name=Ultimaker Cura
GenericName=3D Printing Software
Comment=Cura converts 3D models into paths for a 3D printer. It prepares your print for maximum accuracy, minimum printing time and good reliability with many extra features that make your print come out great.
Exec=bash -c 'sudo modprobe fuse; ~/Cura.AppImage '"'\ "%f"'"
Icon=$(dirname $0)/icon-64.png
Terminal=false
Type=Application
MimeType=application/sla;application/vnd.ms-3mfdocument;application/prs.wavefront-obj;image/bmp;image/gif;image/jpeg;image/png;model/x3d+xml;
Categories=Graphics;Education;Development;Science;
Keywords=3D;Printing;
StartupNotify=true" > ~/.local/share/applications/cura.desktop || error "Failed to create menu button!"

